import java.util.ArrayList;
import java.io.*;
import java.util.Scanner;
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Brian
 */
public class GraphViz {
    
    //output file
    String filename;
    BufferedWriter out;
    //string to be built
    String graph;
    int count;
    
    //accept the list of instructions, with dependencies, and build the graph
    public GraphViz(ArrayList<Instruction> instructions, String fullname) throws IOException
    {
        String rootname;
        if(!fullname.equals(""))
             rootname = fullname.substring(0, fullname.lastIndexOf('.'));
        else
            rootname = "";
        filename = rootname + ".dot";
        count = 0;
        //graph = Count() + ":\tdigraph G {\n";
        graph = "digraph G {\n";
        BuildTree(instructions);
    }
        
    public void BuildTree(ArrayList<Instruction> instructions)
    {
        Instruction temp;
        Dependency dep;
        String last = "";
        //traverse instruction list
        for(int i = 0; i < instructions.size(); i++)
        {
            temp = instructions.get(i);
            
            //label for node
            //graph += Count() + ":\t" + temp.myCount 
            if(!last.equals(""))
            {
                graph +=  temp.myCount
                    + " [shape=rectangle, label=\"L:" + temp.myCount + " " 
                    + temp.name + " " 
                    + temp.Written + " " + temp.Read1 + " " 
                    + temp.Read2 + "\"];\n" ;
                //link previous to this
                graph += last + " -> " + temp.myCount + " [penwidth = 3];\n";
            }
            else
            {
            
                graph += temp.myCount
                        + " [shape=rectangle, label=\"L:" + temp.myCount + " " 
                        + temp.name + " " 
                        + temp.Written + " " + temp.Read1 + " " 
                        + temp.Read2 + "\"];\n" ;
            }
            last = "" + temp.myCount;
            //traverse the list of dependencies, and link to those nodes
            for(int j = 0; j < temp.dependencies.size(); j++)
            {
                //modify color by type
                
                dep = temp.dependencies.get(j);
                String color = "black";
                if(dep.myType.toLowerCase().equals("control"))
                    color = "red"; //#2B0DD5";
                if(dep.myType.equalsIgnoreCase("anti"))
                    color = "blue"; //#9ACEEB";
                if(dep.myType.equalsIgnoreCase("output"))
                    color = "green"; //#FCD975";
                if(dep.myType.equalsIgnoreCase("true data"))
                    color = "orange"; //#D79DD5";
                //graph += Count() + ":\t" + temp.myCount + " -> " 
                graph +=  temp.myCount + " -> "
                        + dep.destination.myCount + " [color = \"" 
                        + color + "\"];\n";
            }
        }
        
        graph += "}";
    }
    
    
    //helper function to insert line numbers
    public int Count()
    {
        count++;
        return count;
    }
    
    public String ToString()
    {
        return graph;
    }
    
    public void Write() throws IOException
    {
        out = new BufferedWriter(new FileWriter(filename));
        out.write(graph);
        out.close();
    }
}
